package com.servingcloud.servera.modules.apiservice;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.servingcloud.common.model.RestPage;
import com.servingcloud.common.model.RestResult;
import com.servingcloud.servera.api.models.request.UserInfoModifyReq;
import com.servingcloud.servera.api.models.request.UserInfoQueryReq;
import com.servingcloud.servera.api.models.request.UserInfoSaveReq;
import com.servingcloud.servera.api.models.response.UserInfoRes;
import com.servingcloud.servera.api.service.UserInfoService;
import com.servingcloud.servera.modules.entity.UserInfo;
import com.servingcloud.servera.modules.model.UserInfoMODIFY;
import com.servingcloud.servera.modules.model.UserInfoQUERY;
import com.servingcloud.servera.modules.model.UserInfoSAVE;
import com.servingcloud.servera.utils.BeanCopierWrapper;
@RestController
public class UserInfoServiceApiImpl implements UserInfoService{

	@Autowired
    private com.servingcloud.servera.modules.service.UserInfoService userInfoService;
    @Autowired HttpServletRequest request;
    private String getHostInfo() {
    	return "servera的地址："+request.getLocalName() +":"+request.getServerPort();
    }
	@Override
	public RestResult<RestPage<UserInfoRes>> findUserInfoPage(@RequestBody UserInfoQueryReq param) {
		RestResult<RestPage<UserInfoRes>> ret = new RestResult<RestPage<UserInfoRes>>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		UserInfoQUERY obj = new UserInfoQUERY();
    		BeanCopierWrapper.copy(param, obj);
    		RestPage<UserInfo> page = userInfoService.findUserInfoPage(obj);
    		RestPage<UserInfoRes> uPage = new RestPage<UserInfoRes>();
    		uPage.setTotal(page.getTotal());
    		if(page.getList() !=null && !page.getList().isEmpty()) {
    			List<UserInfoRes> uList = new ArrayList<UserInfoRes>(page.getList().size());
    			UserInfoRes ur = null;
        		for(UserInfo u:page.getList()) {
        			ur = new UserInfoRes();
        			BeanCopierWrapper.copy(u, ur);
        			uList.add(ur);
        		}
        		uPage.add(uList);
    		}
    		ret.setReturnValue(uPage);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(getHostInfo()+"，发错误，错误信息："+e.getMessage());
    	}
    	return ret;
	}
	@Override
	public RestResult<Long> saveUserInfo(@RequestBody UserInfoSaveReq param) {
		RestResult<Long> ret = new RestResult<Long>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		UserInfoSAVE obj = new UserInfoSAVE();
    		BeanCopierWrapper.copy(param, obj);
    		ret.setReturnValue(userInfoService.saveUserInfo(obj));
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(getHostInfo()+"，发错误，错误信息："+e.getMessage());
    	}
    	return ret;
	}
	@Override
	public RestResult<Object> modifyUserInfo(@RequestBody UserInfoModifyReq param) {
		RestResult<Object> ret = new RestResult<Object>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		UserInfoMODIFY obj = new UserInfoMODIFY();
    		BeanCopierWrapper.copy(param, obj);
    		userInfoService.modifyUserInfo(obj);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(getHostInfo()+"，发错误，错误信息："+e.getMessage());
    	}
    	return ret;
	}
	@Override
	public RestResult<Object> deleteUserInfo(@RequestBody UserInfoModifyReq param) {
		RestResult<Object> ret = new RestResult<Object>();
    	ret.setSuccess(true);
    	ret.setErrorMsg(getHostInfo());
    	try {
    		UserInfoMODIFY obj = new UserInfoMODIFY();
    		BeanCopierWrapper.copy(param, obj);
    		userInfoService.deleteUserInfo(obj);
    	}catch(Exception e) {
    		ret.setSuccess(false);
    		ret.setErrorCode("-1");
    		ret.setErrorMsg(getHostInfo()+"，发错误，错误信息："+e.getMessage());
    	}
    	return ret;
	}
	
}